ADXStatus <- function(TS, n=14, maType="EMA", ADXl=15, ADXu=25, wilder=TRUE, addSig=FALSE) {
  TSADX <- ADX(cbind(Hi(TS),Lo(TS),Cl(TS)), n=n, maType=maType, wilder=wilder)
  LAGTSADX <- LagMultiColumnXTS(TSADX)
  ADXTh1=TSADX[,"ADX"] < ADXl
  ADXTh2=TSADX[,"ADX"] > ADXu
  ADXThreshold=cbind(ADXTh1, !(ADXTh1|ADXTh2), ADXTh2)
  colnames(ADXThreshold) <- c(paste("ADXlt",ADXl,sep=""), "ADXnt", paste("ADXgt",ADXu,sep=""))
  ADXDirection=cbind(TSADX[,"ADX"] > LAGTSADX[,"ADX"], TSADX[,"ADX"] < LAGTSADX[,"ADX"])
  colnames(ADXDirection) <- c("ADXup", "ADXdn")
  ADXTrend <- cbind(TSADX[,"DIp"]>TSADX[,"DIn"], TSADX[,"DIn"]>TSADX[,"DIp"])
  colnames(ADXTrend) <- c("DIpgt", "DIngt")
  if (addSig == TRUE) {
    ADXTrendCross <- cbind(Lag(ADXTrend[,"DIngt"]) & ADXTrend[,"DIpgt"] , Lag(ADXTrend[,"DIpgt"]) & ADXTrend[,"DIngt"])
    colnames(ADXTrendCross) <- c("sig_DIcu", "sig_DIcd")
    ADXDirChange <- cbind(Lag(ADXDirection[,"ADXdn"]) & ADXDirection[,"ADXup"], Lag(ADXDirection[,"ADXup"]) & ADXDirection[,"ADXdn"])
    colnames(ADXDirChange) <- c("sig_ADXrise", "sig_ADXfall")
    return(cbind(ADXThreshold, ADXTrend, ADXTrendCross, ADXDirChange))
  } else {
    return(cbind(ADXThreshold, ADXTrend, ADXDirection))
  }
}

SMAStatus <- function(TS, n=200) {
  TSSMA <- SMA(Cl(TS), n=n)
  TSGTSMA <- Cl(TS) > TSSMA
  TSLTSMA <- Cl(TS) < TSSMA
  result <- cbind(TSGTSMA, TSLTSMA)
  colnames(result) <- c(paste("CgtSMA",n,sep=""), paste("CltSMA",n,sep=""))
  return(result)
}

CSPStatus <- function(TS, patternvec) {
  if ("MARUBOZU_1_3" %in% patternvec) {
    Marubozu <- CSPMarubozu(TS, ATRFactor=1, maxuppershadowCL=.3, maxlowershadowCL=.3)
    colnames(Marubozu) <- c("WhiteMarubozu_1_3", "BlackMarubozu_1_3")
    return(Marubozu)
  }
  if ("MARUBOZU" %in% patternvec) {
    Marubozu <- CSPMarubozu(TS)
    return(Marubozu)
  }
  if ("GAP" %in% patternvec) {
    Gap <- CSPGap(TS)
    return(Gap)
  }
  if ("STOMACH" %in% patternvec) {
    Stomach <- CSPStomach(TS)
    return(Stomach)
  }
  if ("THREE" %in% patternvec) {
    TWS <- CSPThreeWhiteSoldiers(TS, strict=FALSE, minbodysizeMedian = 0.8)
    TBC <- CSPThreeBlackCrows(TS, strict=FALSE, minbodysizeMedian = 0.8)
    return(cbind(TWS,TBC))
  }
}

DCStatus <- function(TS, n) {
  DC <- DonchianChannel2(cbind(Hi(TS), Lo(TS)) , n)
  DCS <- cbind(Cl(TS)>DC[,"high"], Cl(TS)<DC[,"low"])
  DC <- merge(DCS, 0, 0)
  colnames(DC) <- c("A", "B", paste("CgtDC",n,sep=""), paste("CltDC",n,sep=""))
  Stat <- 0
  for (i in 1:dim(DC)[1]) {
    if (!(is.na(DC[i,1]) | is.na(DC[i,2]))) {  
      if (Stat <= 0) {
        if (DC[i,1] >0) {
          DC[i,3] <- 1
          Stat <- 1
        }
      }
      if (Stat >= 0) {
        if (DC[i,2] >0) {
          DC[i,4] <- 1
          Stat <- (-1)
        }
      }
    }
  }
  return(DC[,3:4])
}
